<!DOCTYPE html>
<html lang="en">

<!-- Head tag -->
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="google-site-verification" content="xBT4GhYoi5qRD5tr338pgPM5OWHHIDR6mNg1a3euekI" />
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="description" content="">
    <meta name="keyword"  content="">
    <link rel="shortcut icon" href="/img/logo.png">
    <!-- Place this tag in your head or just before your close body tag. -->
    <script async defer src="https://buttons.github.io/buttons.js"></script>
    <title>
        
          从前往后—我的数据怎么传给后端啊 - 宋正兵的博客 | zbsong Blog
        
    </title>

    <link rel="canonical" href="zbsong.top/article/从前往后—我的数据怎么传给后端啊/">

    <!-- Bootstrap Core CSS -->
    <link rel="stylesheet" href="/css/bootstrap.min.css">

    <!-- Custom CSS --> 
    <link rel="stylesheet" href="/css/beantech.min.css">

    <link rel="stylesheet" href="/css/donate.css">
    
    <!-- Pygments Highlight CSS -->
    <link rel="stylesheet" href="/css/highlight.css">

    <link rel="stylesheet" href="/css/widget.css">

    <link rel="stylesheet" href="/css/rocket.css">

    <link rel="stylesheet" href="/css/signature.css">

    <link rel="stylesheet" href="/css/toc.css">

    <!-- Custom Fonts -->
    <!-- <link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.3.0/css/font-awesome.min.css" rel="stylesheet" type="text/css"> -->
    <!-- Hux change font-awesome CDN to qiniu -->
    <link href="https://cdn.staticfile.org/font-awesome/4.5.0/css/font-awesome.min.css" rel="stylesheet" type="text/css">


    <!-- Hux Delete, sad but pending in China
    <link href='http://fonts.googleapis.com/css?family=Lora:400,700,400italic,700italic' rel='stylesheet' type='text/css'>
    <link href='http://fonts.googleapis.com/css?family=Open+Sans:300italic,400italic,600italic,700italic,800italic,400,300,600,700,800' rel='stylesheet' type='text/
    css'>
    -->


    <!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
    <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
    <!--[if lt IE 9]>
        <script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
        <script src="https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->

    <!-- ga & ba script hoook -->
    <script></script>
</head>


<!-- hack iOS CSS :active style -->
<body ontouchstart="">
	<!-- Modified by Yu-Hsuan Yen -->
<!-- Post Header -->
<style type="text/css">
    header.intro-header{
        
            background-image: url('https://api.dujin.org/bing/1920.php')
            /*post*/
        
    }
    
</style>

<header class="intro-header" >
    <!-- Signature -->
    <div id="signature">
        <div class="container">
            <div class="row">
                <div class="col-lg-8 col-lg-offset-2 col-md-10 col-md-offset-1">
                
                    <div class="post-heading">
                        <div class="tags">
                            
                              <a class="tag" href="/tags/#note" title="note">note</a>
                            
                        </div>
                        <h1>从前往后—我的数据怎么传给后端啊</h1>
                        <!-- <h2 class="subheading">数据从前端通过HTTP请求发送给后端以及后端如何接收</h2> -->
                        <span class="meta">
                            宋正兵 on
                            2020-11-03
                        </span>
                    </div>
                


                </div>
            </div>
        </div>
    </div>
</header>

    <!-- Navigation -->
<nav class="navbar navbar-default navbar-custom navbar-fixed-top">
    <div class="container-fluid">
        <!-- Brand and toggle get grouped for better mobile display -->
        <div class="navbar-header page-scroll">
            <button type="button" class="navbar-toggle">
                <span class="sr-only">Toggle navigation</span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
            </button>
            <a class="navbar-brand" href="/">songzblink</a>
        </div>

        <!-- Collect the nav links, forms, and other content for toggling -->
        <!-- Known Issue, found by Hux:
            <nav>'s height woule be hold on by its content.
            so, when navbar scale out, the <nav> will cover tags.
            also mask any touch event of tags, unfortunately.
        -->
        <div id="huxblog_navbar">
            <div class="navbar-collapse">
                <ul class="nav navbar-nav navbar-right">
                    <li>
                        <a href="/">主页</a>
                    </li>
					<li>
                        <a href="/archive/">归档</a>
                    </li>
					<li>
                        <a href="/tags/">标签</a>
                    </li>
					<li>
                        <a href="/about/">关于</a>
                    </li>
					<!--
					修改about在前面的问题
                    
                        
                    
                        
                        <li>
                            <a href="/about/">关于</a>
                        </li>
                        
                    
                        
                        <li>
                            <a href="/archive/">归档</a>
                        </li>
                        
                    
                        
                        <li>
                            <a href="/tags/">标签</a>
                        </li>
                        
                    
                    -->
                </ul>
            </div>
        </div>
        <!-- /.navbar-collapse -->
    </div>
    <!-- /.container -->
</nav>
<script>
    // Drop Bootstarp low-performance Navbar
    // Use customize navbar with high-quality material design animation
    // in high-perf jank-free CSS3 implementation
    var $body   = document.body;
	// querySelector 获取文档中 id="demo" 的元素
    var $toggle = document.querySelector('.navbar-toggle');
    var $navbar = document.querySelector('#huxblog_navbar');
    var $collapse = document.querySelector('.navbar-collapse');

    $toggle.addEventListener('click', handleMagic)
    function handleMagic(e){
        if ($navbar.className.indexOf('in') > 0) {
        // CLOSE
            $navbar.className = " ";
            // wait until animation end.
            setTimeout(function(){
                // prevent frequently toggle
                if($navbar.className.indexOf('in') < 0) {
                    $collapse.style.height = "0px"
                }
            },400)
        }else{
        // OPEN
            $collapse.style.height = "auto"
            $navbar.className += " in";
        }
    }
</script>


    <!-- Main Content -->
    <!-- Modify by Yu-Hsuan Yen -->

<!-- Post Content -->
<article>
    <div class="container">
        <div class="row">

            <!-- Post Container -->
            <div class="
                col-lg-8 col-lg-offset-2
                col-md-10 col-md-offset-1
                post-container">

				
                <h2 id="前端通过http请求发送数据给后端">前端通过HTTP请求发送数据给后端</h2>
<p>客户端想要提交HTTP请求给Web服务器，通常都需要带有参数，包括：</p>
<ul>
<li>URL：页面地址</li>
<li>METHOD：页面的提交方法，GET、POST等</li>
<li>Content-Type：HTTP请求提交的内容类型。一般只有post提交时才需要设置该属性</li>
<li>Body：存储POST的参数和参数数据</li>
</ul>
<p><img src="https://pic.tyzhang.top/images/2020/11/09/QGYHF83kTVIB9Lz.png" alt="QGYHF83kTVIB9Lz.png"></p>
<p>根据HTTP的请求方法对应的数据传输能力，把HTTP请求分为Url类请求和Body请求：</p>
<ul>
<li>Url类请求：包括但不限于GET、HEAD、OPTIONS、TRACE等请求方法；</li>
<li>Body类请求：包括但不限于POST、PUSH、PATCH、DELETE等请求方法；</li>
</ul>
<h3 id="url类请求释义">URL类请求释义</h3>
<p>对于Url类请求，客户端是不能通过body写出数据的，服务端拿到的body是空的。因此一般情况下Url类的请求想要提交参数，就把参数跟在url后边，url一般是指向资源的，后面跟的参数更多的是一种条件和要求。</p>
<p><img src="https://pic.tyzhang.top/images/2020/11/09/vHj867wQxV1Ahg5.png" alt="vHj867wQxV1Ahg5.png"></p>
<p>我们把参数的这一段整体叫做Query，把它们拆开后组成键值对的形式又叫做Parameters，大多数情况下一个Key对应一个Value，当然是允许一个Key对应多个Values的。</p>
<h3 id="body类请求释义">Body类请求释义</h3>
<p>对于Body类请求，客户端可以通过body写出数据，服务端可以通过body接收数据。也正因此Body类请求能上传到服务端的数据比Url类请求的大得多。</p>
<p>Body类请求除了可以把参数放到Url中，也可以通过body发送数据。</p>
<p>例如当Content-Type是 <code>application/x-www-form-urlencoded</code> 时，客户端可以通过body发送键值对数据，也可以通过url传参数，例如 <code>key1=value1&amp;key2=value2</code>；</p>
<p>例如当Content-Type是 <code>application/json</code>时，客户端可以通过body发送json字符串，如果是 <code>application/xml</code> 时可以发送xml字符串；</p>
<p>例如当Content-Type是 <code>application/octet-stream</code> 时，客户端可以通过body发送Binary数据，例如文件或者任何能转成流的数据；</p>
<p>例如当Content-Type是 <code>multipart/form-data</code> 时，客户端可以通过body发送一个表单。</p>
<blockquote>
<p>注：表单是有格式的，不像json和xml格式，json和xml的格式是字符串排版格式，而表单的格式里面既有字符串又有换行和Binary。</p>
</blockquote>
<h2 id="前端http请求实例">前端HTTP请求实例</h2>
<p>在这里仅针对Url类请求发送数据和Body类请求通过body发送数据做演示。后端在得到前端传递的数据后会再每个参数后面添加请求类型的名称，再返回给客户端。</p>
<h3 id="url类请求发送数据url和参数拼接">Url类请求发送数据——url和参数拼接</h3>
<p>以GET请求为例，发送用户的登录信息username和password给后端。</p>
<p>因为对于Url类请求，客户端不能通过body写出数据，所以我们需要把url和参数进行拼接，代码如下：</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">$(<span class="built_in">document</span>).ready(<span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">    $(<span class="string">"button"</span>).click(<span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">        <span class="comment">// get请求</span></span><br><span class="line">        $.ajax(&#123;</span><br><span class="line">            url: <span class="string">"/login?username="</span> + $(<span class="string">"#username"</span>).val() + <span class="string">"&amp;password="</span> + $(<span class="string">"#password"</span>).val(),</span><br><span class="line">            success: <span class="function"><span class="keyword">function</span> (<span class="params">data</span>) </span>&#123;</span><br><span class="line">                <span class="built_in">console</span>.log(data);</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;)</span><br><span class="line">    &#125;)</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>
<p>GET请求报文：</p>
<p><img src="https://pic.tyzhang.top/images/2020/11/09/58weCPzTStVXUc2.png" alt="58weCPzTStVXUc2.png"></p>
<p>后端在接收到前端的GET请求之后，会在username和password参数后面添加字符串 <code>--get</code>，请求成功之后，在控制台打印返回的数据。</p>
<p><img src="https://pic.tyzhang.top/images/2020/11/09/YWDVyXHpE5MP4mt.png" alt="YWDVyXHpE5MP4mt.png"></p>
<h3 id="body类请求发送数据指定为applicationjson格式放在body中">Body类请求发送数据——指定为application/json格式，放在body中</h3>
<p>一般而言，对于Body类请求都习惯把数据存放到body中，大部分是以json格式。</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 将表单的数据进行封装成JavaScript值，再转换为json字符串</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">formToJsonObject</span>(<span class="params">form</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">var</span> a,o,h,i,e;</span><br><span class="line">    a = form.serializeArray();</span><br><span class="line">    o=&#123;&#125;;</span><br><span class="line">    h=o.hasOwnProperty;</span><br><span class="line">    <span class="keyword">for</span>(i=<span class="number">0</span>;i&lt;a.length;i++)&#123;</span><br><span class="line">        e=a[i];</span><br><span class="line">        <span class="keyword">if</span>(!h.call(o,e.name))&#123;</span><br><span class="line">            o[e.name]=e.value;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> <span class="built_in">JSON</span>.stringify(o);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">$(<span class="built_in">document</span>).ready(<span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">    $(<span class="string">"button"</span>).click(<span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">        <span class="comment">// post请求</span></span><br><span class="line">        $.ajax(&#123;</span><br><span class="line">            url: <span class="string">"/login"</span>,</span><br><span class="line">            type: <span class="string">"POST"</span>,</span><br><span class="line">            contentType: <span class="string">"application/json;charset=utf-8"</span>,</span><br><span class="line">            data: formToJsonObject($(<span class="string">"#loginForm"</span>)),</span><br><span class="line">            success: <span class="function"><span class="keyword">function</span> (<span class="params">data</span>) </span>&#123;</span><br><span class="line">                <span class="built_in">console</span>.log(data);</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;)</span><br><span class="line">    &#125;)</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>
<p>POST请求报文：</p>
<p><img src="https://pic.tyzhang.top/images/2020/11/09/jvwDC3IPOYmhrWU.png" alt="jvwDC3IPOYmhrWU.png"></p>
<p>后端在接收到前端的POST请求之后，会在username和password参数后面添加字符串 <code>--post</code>，请求成功之后，在控制台打印返回的数据。</p>
<p><img src="https://pic.tyzhang.top/images/2020/11/09/FU1wJjIX2RuyNso.png" alt="FU1wJjIX2RuyNso.png"></p>
<h2 id="后端接收http请求实例">后端接收HTTP请求实例</h2>
<blockquote>
<p>在整个实例中，我们都添加@ResponseBody注解，将java对象转为json格式的数据返回。</p>
</blockquote>
<h3 id="接收url请求的数据">接收Url请求的数据</h3>
<p>我们以前端HTTP请求的Url类请求实例为例，url为 <code>http://localhost:8080/login?username=root&amp;password=123456</code>，传递的参数在query字段为 <code>username=root&amp;password=123456</code>。</p>
<p>我们已知客户端的请求类型有以下几种接收方式：</p>
<h4 id="1直接获取基本的数据类型">1）直接获取基本的数据类型</h4>
<p>Controller中的业务方法的参数名称要与请求参数的name一致，参数值会自动映射匹配。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@GetMapping</span>(<span class="string">"login"</span>)</span><br><span class="line"><span class="meta">@ResponseBody</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> User <span class="title">getLogin</span><span class="params">(String username, String password)</span> </span>&#123;</span><br><span class="line">    System.out.println(<span class="string">"get"</span>);</span><br><span class="line">    System.out.println(<span class="string">"username="</span> + username + <span class="string">"、password="</span> + password);</span><br><span class="line">    User user = <span class="keyword">new</span> User();</span><br><span class="line">    user.setUsername(username + <span class="string">"--get"</span>);</span><br><span class="line">    user.setPassword(password + <span class="string">"--get"</span>);</span><br><span class="line">    <span class="keyword">return</span> user;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h4 id="2获得pojo类型参数">2）获得POJO类型参数</h4>
<p>Controller中的业务方法的POJO参数的属性名与请求参数的name一致，参数值会自动映射匹配。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@GetMapping</span>(<span class="string">"login"</span>)</span><br><span class="line"><span class="meta">@ResponseBody</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> User <span class="title">getLogin</span><span class="params">(User user)</span> </span>&#123;</span><br><span class="line">    System.out.println(<span class="string">"get"</span>);</span><br><span class="line">    System.out.println(user.toString());</span><br><span class="line">    user.setUsername(user.getUsername()+<span class="string">"--get"</span>);</span><br><span class="line">    user.setPassword(user.getPassword()+<span class="string">"--get"</span>);</span><br><span class="line">    <span class="keyword">return</span> user;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="接收body类请求的数据">接收Body类请求的数据</h3>
<p>我们以前端HTTP请求的Body类请求实例为例，url为 <code>http://localhost:8080/login</code>，body内容为 <code>{&quot;username&quot;:&quot;root&quot;,&quot;password&quot;:&quot;123456&quot;}</code>。</p>
<p>由于传递的参数是JSON格式，所以Controller中的业务方法的POJO参数必须添加注解 <code>@RequestBody</code>，才能正常接收数据。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@PostMapping</span>(<span class="string">"login"</span>)</span><br><span class="line"><span class="meta">@ResponseBody</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> User <span class="title">postLogin</span><span class="params">(@RequestBody User user)</span> </span>&#123;</span><br><span class="line">    System.out.println(<span class="string">"post"</span>);</span><br><span class="line">    System.out.println(user.toString());</span><br><span class="line">    user.setUsername(user.getUsername()+<span class="string">"--post"</span>);</span><br><span class="line">    user.setPassword(user.getPassword()+<span class="string">"--post"</span>);</span><br><span class="line">    <span class="keyword">return</span> user;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<blockquote>
<p>如果User类中不仅忧username和password属性，也可以用来接收body中的username和password参数</p>
</blockquote>
<h2 id="总结">总结</h2>
<p>前后端数据通信的时候先确定请求的方式。如果是Url类请求，直接将参数拼接到url的query字段是很方便的；如果是Body类请求，则还要确认需要传输的内容：</p>
<p>如果只是普通的数据，比如可以明文的数据，那么没有必要将其封装到body中进行传输，直接将其以 <code>key1=value1&amp;key2=value2</code> 的格式提交给后台是很方便的；</p>
<p>如果是表单数据，当表单内容较多时如果还是将其拼接到url后面，整个url将不是一般的长，又或者是不想让它明文显示在url中的数据，那么将其封装到body中是一个不错的选择。</p>
<blockquote>
<p>如果使用基本类型去接收JSON字符串，将不能正常完成请求。</p>
<p>所以进来使用POJO类型。</p>
</blockquote>
<p>我总结出两种常用的方式：</p>
<ul>
<li>
<p>Url类请求：</p>
<p>请求参数以 <code>key1=value1&amp;key2=value2</code> 的格式拼接到url后面，提交给后台。后台可以通过@RequestParam或者基本类型和POJO类来进行基本类型参数的接收。（基本类型和POJO类进行接收需要确认属性名和请求参数名称相同）</p>
</li>
<li>
<p>Body类请求：</p>
<p>请求参数以JSON格式封装到body中提交给后台。后台通过@RequestBody接收数据。</p>
</li>
</ul>
<h3 id="requstbody与requestparam的区别">@RequstBody与@RequestParam的区别</h3>
<ul>
<li>spring的@RequestParam注解接收的参数是来自于requestHeader中，即请求头。都是用来获取请求路径（url ）中的动态参数。也就是在url中，格式为 <code>xxx?username=123&amp;password=456</code> 。功能与@pathvarible类似。</li>
<li>RequestBody注解接收的参数则是来自于requestBody中，即请求体中。</li>
</ul>
<h3 id="requestbody的加与不加的区别">@RequestBody的加与不加的区别</h3>
<ul>
<li>使用@requestBody。当请求contentType为：application/json类型的请求，数据类型为json时， json格式如下： <code>{&quot;username&quot;:&quot;root&quot;,&quot;password&quot;:&quot;123456&quot;}</code></li>
<li>不使用@RequestBody.当请求contentType为：application/x-www-form-urlencoded类型的或multipart/form-data时，数据格式为 <code>username=root&amp;password=123456</code>。</li>
</ul>
<h3 id="jquery的ajaxurlsettings">JQuery的$.ajax(url,[settings])</h3>
<ol>
<li>默认的ContentType的值为: <code>application/x-www-form-urlencoded; charset=UTF-8</code>此格式为表单提交格式，数据为 <code>key1=value1&amp;key2=value2</code> 的格式 。</li>
<li>虽然ajax的data属性值格式为: <code>{key1:value1,key2:value2}</code> ,但最后会转为 <code>key1=value1&amp;key2=value2</code> 的格式提交到后台 。</li>
<li>如果ajax要和SpringMVC交互，要使用 <code>key1=value1&amp;key2=value2</code> 的格式，后台SpringMVC只需要定义对象或者参数就行了，会自动映射（不进行配置的话，自动映射的前提是请求参数的名称和属性名称相同）。<br>
4.如果SpringMVC的参数有@RequestBody注解（接收json<strong>字符串</strong>格式数据），ajax必须将date属性值转为json字符串，不能为json对象（js对象，会自动转为key=value形式）。并且，修改contentType的值为: <code>application/json; charset=UTF-8</code> ,这样加了@RequestBody注解的属性才能自定映射到值。<br>
5.使用在进行图片或者文件上传时使用 multipart/form-data 类型时、 数据会自动进行映射不要添加任何注解。</li>
</ol>
<blockquote>
<p>找机会开个坑把从后往前写了。</p>
</blockquote>
<p>参考资料：</p>
<ul>
<li>Post 方法参数写在body中和写在url中有什么区别？ - 严振杰的回答 - 知乎 <a href="https://www.zhihu.com/question/64312188/answer/370779721" target="_blank" rel="noopener">https://www.zhihu.com/question/64312188/answer/370779721</a></li>
<li>@requestBody 与@requestparam；@requestBody的加与不加的区别 <a href="https://blog.csdn.net/jiashanshan521/article/details/88244735" target="_blank" rel="noopener">https://blog.csdn.net/jiashanshan521/article/details/88244735</a></li>
</ul>

                
                <hr>
                <!-- Pager -->
                <ul class="pager">
                    
                        <li class="previous">
                            <a href="/article/jdk学习之路—重写equals时必须重写hashCode方法/" data-toggle="tooltip" data-placement="top" title="重写equals时必须重写hashCode方法">&larr; 上一篇</a>
                        </li>
                    
                    
                        <li class="next">
                            <a href="/article/jdk学习之路—Integer的缓存机制/" data-toggle="tooltip" data-placement="top" title="Integer的缓存机制">下一篇 &rarr;</a>
                        </li>
                    
                </ul>

                <br>

                <!--打赏-->
                
                <!--打赏-->

                <br>
                <!--分享-->
                
                <!--分享-->
                <br>                       
                
                <!-- require APlayer -->
                
            </div>
            
            <!-- Tabe of Content -->
            <!-- Table of Contents -->

  
    <style>
      span.toc-nav-number{
        display: none
      }
    </style>
  
    
      <aside id="sidebar">
        <div id="toc" class="toc-article">
        <strong class="toc-title">目录</strong>
        
          <ol class="toc-nav"><li class="toc-nav-item toc-nav-level-2"><a class="toc-nav-link" href="#前端通过http请求发送数据给后端"><span class="toc-nav-number">1.</span> <span class="toc-nav-text">&#x524D;&#x7AEF;&#x901A;&#x8FC7;HTTP&#x8BF7;&#x6C42;&#x53D1;&#x9001;&#x6570;&#x636E;&#x7ED9;&#x540E;&#x7AEF;</span></a><ol class="toc-nav-child"><li class="toc-nav-item toc-nav-level-3"><a class="toc-nav-link" href="#url类请求释义"><span class="toc-nav-number">1.1.</span> <span class="toc-nav-text">URL&#x7C7B;&#x8BF7;&#x6C42;&#x91CA;&#x4E49;</span></a></li><li class="toc-nav-item toc-nav-level-3"><a class="toc-nav-link" href="#body类请求释义"><span class="toc-nav-number">1.2.</span> <span class="toc-nav-text">Body&#x7C7B;&#x8BF7;&#x6C42;&#x91CA;&#x4E49;</span></a></li></ol></li><li class="toc-nav-item toc-nav-level-2"><a class="toc-nav-link" href="#前端http请求实例"><span class="toc-nav-number">2.</span> <span class="toc-nav-text">&#x524D;&#x7AEF;HTTP&#x8BF7;&#x6C42;&#x5B9E;&#x4F8B;</span></a><ol class="toc-nav-child"><li class="toc-nav-item toc-nav-level-3"><a class="toc-nav-link" href="#url类请求发送数据url和参数拼接"><span class="toc-nav-number">2.1.</span> <span class="toc-nav-text">Url&#x7C7B;&#x8BF7;&#x6C42;&#x53D1;&#x9001;&#x6570;&#x636E;&#x2014;&#x2014;url&#x548C;&#x53C2;&#x6570;&#x62FC;&#x63A5;</span></a></li><li class="toc-nav-item toc-nav-level-3"><a class="toc-nav-link" href="#body类请求发送数据指定为applicationjson格式放在body中"><span class="toc-nav-number">2.2.</span> <span class="toc-nav-text">Body&#x7C7B;&#x8BF7;&#x6C42;&#x53D1;&#x9001;&#x6570;&#x636E;&#x2014;&#x2014;&#x6307;&#x5B9A;&#x4E3A;application/json&#x683C;&#x5F0F;&#xFF0C;&#x653E;&#x5728;body&#x4E2D;</span></a></li></ol></li><li class="toc-nav-item toc-nav-level-2"><a class="toc-nav-link" href="#后端接收http请求实例"><span class="toc-nav-number">3.</span> <span class="toc-nav-text">&#x540E;&#x7AEF;&#x63A5;&#x6536;HTTP&#x8BF7;&#x6C42;&#x5B9E;&#x4F8B;</span></a><ol class="toc-nav-child"><li class="toc-nav-item toc-nav-level-3"><a class="toc-nav-link" href="#接收url请求的数据"><span class="toc-nav-number">3.1.</span> <span class="toc-nav-text">&#x63A5;&#x6536;Url&#x8BF7;&#x6C42;&#x7684;&#x6570;&#x636E;</span></a><ol class="toc-nav-child"><li class="toc-nav-item toc-nav-level-4"><a class="toc-nav-link" href="#1直接获取基本的数据类型"><span class="toc-nav-number">3.1.1.</span> <span class="toc-nav-text">1&#xFF09;&#x76F4;&#x63A5;&#x83B7;&#x53D6;&#x57FA;&#x672C;&#x7684;&#x6570;&#x636E;&#x7C7B;&#x578B;</span></a></li><li class="toc-nav-item toc-nav-level-4"><a class="toc-nav-link" href="#2获得pojo类型参数"><span class="toc-nav-number">3.1.2.</span> <span class="toc-nav-text">2&#xFF09;&#x83B7;&#x5F97;POJO&#x7C7B;&#x578B;&#x53C2;&#x6570;</span></a></li></ol></li><li class="toc-nav-item toc-nav-level-3"><a class="toc-nav-link" href="#接收body类请求的数据"><span class="toc-nav-number">3.2.</span> <span class="toc-nav-text">&#x63A5;&#x6536;Body&#x7C7B;&#x8BF7;&#x6C42;&#x7684;&#x6570;&#x636E;</span></a></li></ol></li><li class="toc-nav-item toc-nav-level-2"><a class="toc-nav-link" href="#总结"><span class="toc-nav-number">4.</span> <span class="toc-nav-text">&#x603B;&#x7ED3;</span></a><ol class="toc-nav-child"><li class="toc-nav-item toc-nav-level-3"><a class="toc-nav-link" href="#requstbody与requestparam的区别"><span class="toc-nav-number">4.1.</span> <span class="toc-nav-text">@RequstBody&#x4E0E;@RequestParam&#x7684;&#x533A;&#x522B;</span></a></li><li class="toc-nav-item toc-nav-level-3"><a class="toc-nav-link" href="#requestbody的加与不加的区别"><span class="toc-nav-number">4.2.</span> <span class="toc-nav-text">@RequestBody&#x7684;&#x52A0;&#x4E0E;&#x4E0D;&#x52A0;&#x7684;&#x533A;&#x522B;</span></a></li><li class="toc-nav-item toc-nav-level-3"><a class="toc-nav-link" href="#jquery的ajaxurlsettings"><span class="toc-nav-number">4.3.</span> <span class="toc-nav-text">JQuery&#x7684;$.ajax(url,[settings])</span></a></li></ol></li></ol>
        
        </div>
      </aside>
    

                
            <!-- Sidebar Container -->
            <div class="
                col-lg-8 col-lg-offset-2
                col-md-10 col-md-offset-1
                sidebar-container">

                <!-- Featured Tags -->
                
                <section>
                    <!-- no hr -->
                    <h5><a href="/tags/">标签</a></h5>
                    <div class="tags">
                       
                          <a class="tag" href="/tags/#note" title="note">note</a>
                        
                    </div>
                </section>
                

                <!-- Friends Blog -->
				<!--
                
                <hr>
                <h5>朋友们</h5>
                <ul class="list-inline">

                    
                        <li><a href="https://tyzhang.top/" target="_blank">ztygalaxy</a></li>
                    
                        <li><a href="http://www.yctang.club/" target="_blank">yctang</a></li>
                    
                        <li><a href="https://dolnw.github.io" target="_blank">JCWang</a></li>
                    
                </ul>
                
				-->
            </div>
        </div>
    </div>
</article>


<!-- async load function -->
<script>
    function async(u, c) {
      var d = document, t = 'script',
          o = d.createElement(t),
          s = d.getElementsByTagName(t)[0];
      o.src = u;
      if (c) { o.addEventListener('load', function (e) { c(null, e); }, false); }
      s.parentNode.insertBefore(o, s);
    }
</script>
<!-- anchor-js, Doc:http://bryanbraun.github.io/anchorjs/ -->
<script>
    async("https://cdn.bootcss.com/anchor-js/1.1.1/anchor.min.js",function(){
        anchors.options = {
          visible: 'hover',
          placement: 'left',
          icon: ''
        };
        anchors.add().remove('.intro-header h1').remove('.subheading').remove('.sidebar-container h5');
    })
</script>
<style>
    /* place left on bigger screen */
    @media all and (min-width: 800px) {
        .anchorjs-link{
            position: absolute;
            left: -0.75em;
            font-size: 1.1em;
            margin-top : -0.1em;
        }
    }
</style>


<!-- chrome Firefox 中文锚点定位失效-->
<script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.js"></script>
<!-- smooth scroll behavior polyfill  -->
<script type="text/javascript" src="/js/smoothscroll.js"></script>
<script>
        $('#toc').on('click','a',function(a){
            // var isChrome = window.navigator.userAgent.indexOf("Chrome") !== -1;
            // console.log(window.navigator.userAgent,isChrome)
                // if(isChrome) {
                    // console.log(a.currentTarget.outerHTML);
                    // console.log($(a.currentTarget).attr("href"));
                    //跳转到指定锚点
                    // document.getElementById(a.target.innerText.toLowerCase()).scrollIntoView(true);
                    document.getElementById($(a.currentTarget).attr("href").replace("#","")).scrollIntoView({behavior: 'smooth' });
                // }
        })  
</script>


    <!-- Footer -->
    <!-- Footer -->
<footer>
    <div class="container">
        <div class="row">
            <div class="col-lg-8 col-lg-offset-2 col-md-10 col-md-offset-1">
                <ul class="list-inline text-center">
                
                
                
				
                

                

                
                    <li>
                        <a target="_blank"  href="https://github.com/songzblink">
                            <span class="fa-stack fa-lg">
                                <i class="fa fa-circle fa-stack-2x"></i>
                                <i class="fa fa-github fa-stack-1x fa-inverse"></i>
                            </span>
                        </a>
                    </li>
                

                

                </ul>
                <p class="copyright text-muted">
                    Copyright &copy; 宋正兵 2021 
                    <br>
                    Theme by <a href="http://www.huweihuang.com">Huwei Huang</a>
                    re-Ported by <a href="https://github.com/ztygalaxy">ztygalaxy</a>
					
                </p>
            </div>
        </div>
    </div>
</footer>

<!-- jQuery -->
<script src="/js/jquery.min.js"></script>

<!-- Bootstrap Core JavaScript -->
<script src="/js/bootstrap.min.js"></script>

<!-- Custom Theme JavaScript -->
<script src="/js/hux-blog.min.js"></script>


<!-- async load function -->
<script>
    function async(u, c) {
      var d = document, t = 'script',
          o = d.createElement(t),
          s = d.getElementsByTagName(t)[0];
      o.src = u;
      if (c) { o.addEventListener('load', function (e) { c(null, e); }, false); }
      s.parentNode.insertBefore(o, s);
    }
</script>

<!-- 
     Because of the native support for backtick-style fenced code blocks 
     right within the Markdown is landed in Github Pages, 
     From V1.6, There is no need for Highlight.js, 
     so Huxblog drops it officially.

     - https://github.com/blog/2100-github-pages-now-faster-and-simpler-with-jekyll-3-0  
     - https://help.github.com/articles/creating-and-highlighting-code-blocks/    
-->
<!--
    <script>
        async("http://cdn.bootcss.com/highlight.js/8.6/highlight.min.js", function(){
            hljs.initHighlightingOnLoad();
        })
    </script>
    <link href="http://cdn.bootcss.com/highlight.js/8.6/styles/github.min.css" rel="stylesheet">
-->


<!-- jquery.tagcloud.js -->
<script>
    // only load tagcloud.js in tag.html
    if($('#tag_cloud').length !== 0){
        async("zbsong.top/js/jquery.tagcloud.js",function(){
            $.fn.tagcloud.defaults = {
                //size: {start: 1, end: 1, unit: 'em'},
                color: {start: '#bbbbee', end: '#0085a1'},
            };
            $('#tag_cloud a').tagcloud();
        })
    }
</script>

<!--fastClick.js -->
<script>
    async("https://cdn.bootcss.com/fastclick/1.0.6/fastclick.min.js", function(){
        var $nav = document.querySelector("nav");
        if($nav) FastClick.attach($nav);
    })
</script>


<!-- Google Analytics -->




<!-- Baidu Tongji -->






	<a id="rocket" href="#top" class=""></a>
	<script type="text/javascript" src="/js/totop.js?v=1.0.0" async=""></script>
    <script type="text/javascript" src="/js/toc.js?v=1.0.0" async=""></script>
<!-- Image to hack wechat -->
<img src="zbsong.top/img/icon_wechat.png" width="0" height="0" />
<!-- Migrate from head to bottom, no longer block render and still work -->

<script type="text/x-mathjax-config">
    MathJax.Hub.Config({
        tex2jax: {
            inlineMath: [ ["$","$"], ["\\(","\\)"] ],
            skipTags: ['script', 'noscript', 'style', 'textarea', 'pre', 'code'],
            processEscapes: true
        }
    });
    MathJax.Hub.Queue(function() {
        var all = MathJax.Hub.getAllJax();
        for (var i = 0; i < all.length; ++i)
            all[i].SourceElement().parentNode.className += ' has-jax';
    });
</script>
<!--
<script src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>
-->
<script src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.1/MathJax.js?config=TeX-MML-AM_CHTML"></script>
</body>

</html>
